﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using System.Web;
namespace WinnerCMS.Common
{
    /// <summary>
    /// winrar处理类
    /// </summary>
    public class Winrar
    {
        /// <summary>
        /// 是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        static public bool Exists()
        {
            RegistryKey theReg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            return theReg != null && !string.IsNullOrEmpty(theReg.GetValue("").ToString());
        }
        /// <summary>
        /// 打包成Rar
        /// </summary>
        /// <param name="patch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        public string Compress(string patch, string rarPatch, string rarName)
        {
            try
            {
                RegistryKey theReg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                if (theReg != null)
                {
                    object theObj = theReg.GetValue("");
                    string theRar = theObj.ToString();
                    theReg.Close();
                    theRar = theRar.Substring(1, theRar.Length - 7);
                    Directory.CreateDirectory(patch);
                    //命令参数
                    //the_Info = " a    " + rarName + "  " + @"C:Test?70821.txt"; //文件压缩
                    string theInfo = " a " + rarName + " " + patch + " -r"; ;
                    ProcessStartInfo theStartInfo = new ProcessStartInfo
                    {
                        FileName = theRar,
                        Arguments = theInfo,
                        WindowStyle = ProcessWindowStyle.Hidden,
                        WorkingDirectory = rarPatch
                    };
                    //打包文件存放目录
                    Process theProcess = new Process();
                    theProcess.StartInfo = theStartInfo;
                    theProcess.Start();
                    theProcess.WaitForExit();
                    theProcess.Close();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return string.Empty;
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="unRarPatch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        /// <returns></returns>
        public string UnCompress(string unRarPatch, string rarPatch, string rarName)
        {
            try
            {
                RegistryKey theReg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                if (theReg != null)
                {
                    object theObj = theReg.GetValue("");
                    string theRar = theObj.ToString();
                    theReg.Close();
                    //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                    if (Directory.Exists(unRarPatch) == false)
                    {
                        Directory.CreateDirectory(unRarPatch);
                    }
                    string theInfo = "x \"" + rarName + "\" \"" + unRarPatch + "\" -y";

                    ProcessStartInfo theStartInfo = new ProcessStartInfo
                    {
                        FileName = theRar,
                        Arguments = theInfo,
                        WindowStyle = ProcessWindowStyle.Hidden,
                        WorkingDirectory = rarPatch
                    };

                    Process theProcess = new Process
                    {
                        StartInfo = theStartInfo
                    };
                    theProcess.Start();
                    theProcess.WaitForExit();
                    theProcess.Close();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return string.Empty;
        }
    }
}